home *** CD-ROM | disk | FTP | other *** search
/ Hottest 6 / Hottest 6 (1996)(PDSoft)[!].iso / software / emulators / vgb_amiga_0.3 / amiga.c next >
C/C++ Source or Header  |  1978-11-24  |  6KB  |  204 lines

  1. /** VGB: portable GameBoy emulator ***************************/
  2. /**                                                         **/
  3. /**                           Amiga.c                       **/
  4. /**                                                         **/
  5. /** Amiga-specific graphics and joystick routines           **/
  6. /** drivers.                                                **/
  7. /**                                                         **/
  8. /** Copyright (C) Michael Boese & Matthias Bethke 1995      **/
  9. /** based on the X11 version by Marat Fayzullin and         **/
  10. /** Elan Feingold                                           **/
  11. /**     You are not allowed to distribute this software     **/
  12. /**     commercially. Please, notify me, if you make any    **/
  13. /**     changes to this file.                               **/
  14. /*************************************************************/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <signal.h>
  20.  
  21. #include "GB.h"
  22. #include "rawkeys.h"
  23.  
  24.  
  25. /** Various variables and short functions ********************/
  26. #define WIDTH  160
  27. #define HEIGHT 144
  28.  
  29. char *Title = "VGB Amiga V0.3";
  30.  
  31. int  ExitFlag = 0;
  32. int  SaveCPU  = 1;
  33.  
  34. byte *XBuf,*LinePtr;
  35.  
  36. struct Screen *scr;
  37. struct Window *win;
  38. struct RastPort *rp;
  39. UBYTE Palette[4]={1,2,3,4};
  40.  
  41. char ReqText[]= "Virtual Gameboy V0.2\n\
  42. Amiga version by\n\
  43. Michael Boese & Matthias Bethke\n\n\
  44. In-game keys:\n\
  45. 'x'   -   Button 'A'\n\
  46. 'y'   -   Button 'B'\n\
  47. '.'   -   'Select'\n\
  48. '-'   -   'Start'\n\
  49. All 4 buttons simultaneously: RESET\n\n\
  50. Cursorkeys: Joypad";
  51.  
  52. struct EasyStruct HelpReq =
  53. {
  54.     sizeof(struct EasyStruct),
  55.     0,
  56.     "VGB Amiga Help",
  57.     ReqText,
  58.     "Like it!"
  59. };
  60.  
  61.  
  62. int InitMachine()
  63. {
  64.     if(scr = LockPubScreen(NULL))
  65.     {
  66.         if(win = OpenWindowTags(NULL,
  67.                     WA_Left,(scr->Width - WIDTH)/2,
  68.                     WA_Top,(scr->Height - HEIGHT)/2,
  69.                     WA_InnerWidth,WIDTH,
  70.                     WA_InnerHeight,HEIGHT,
  71.                     WA_Title,Title,
  72.                     WA_GimmeZeroZero,TRUE,
  73.                     WA_CloseGadget,TRUE,
  74.                     WA_DragBar,TRUE,
  75.                     WA_SizeGadget,FALSE,
  76.                     WA_RMBTrap,TRUE,
  77.                     WA_Activate,TRUE,
  78.                     WA_IDCMP,IDCMP_RAWKEY|IDCMP_CLOSEWINDOW,
  79.                     TAG_END
  80.             ))
  81.         {
  82.             UnlockPubScreen(NULL,scr);
  83.             rp = win->RPort;
  84.             AllocatePens();
  85.             if(XBuf=AllocVec(sizeof(byte)*HEIGHT*WIDTH,MEMF_ANY)) return(1);
  86.         } else
  87.         {
  88.             fprintf(stderr,"Can't open window :(((\n");
  89.         }
  90.     } else
  91.     {
  92.         fprintf(stderr,"No default PubScreen!\n");
  93.     }
  94. }
  95.  
  96. /** TrashMachine *********************************************/  
  97. /** Deallocate all resources taken by InitMachine().        **/
  98. /*************************************************************/
  99. void TrashMachine(void)
  100. {
  101. int i;
  102.     FreeVec(XBuf);
  103.     CloseWindow(win);
  104.     for(i=0; i<4; i++) ReleasePen(scr->ViewPort.ColorMap,Palette[i]);
  105. }
  106.  
  107.  
  108. /** PutImage *************************************************/
  109. /** Put an image on the screen.                             **/
  110. /*************************************************************/
  111. void PutImage(void)
  112. {
  113.     WriteChunkyPixels(rp,0,0,WIDTH,HEIGHT,XBuf,160);
  114. }
  115.  
  116.  
  117. /** Joystick *************************************************/
  118. /** Return the current joystick state.                      **/
  119. /*************************************************************/
  120. byte Joystick(void)
  121. {
  122. struct IntuiMessage *imsg;
  123. ULONG imsg_Class;
  124. UWORD Code;
  125. int J;
  126. static byte JoyState=0xff;
  127.  
  128.         if(imsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  129.         {
  130.             imsg_Class = imsg->Class;
  131.             Code  = imsg->Code;
  132.             ReplyMsg((struct Message*)imsg);
  133.             switch(imsg_Class)
  134.             {
  135.                 case IDCMP_CLOSEWINDOW    :    ExitFlag = 1;
  136.                                                     break;
  137.                 case IDCMP_RAWKEY            :
  138.                     if(Code & 0x80)
  139.                     {
  140.                         switch(Code & 0x7f)
  141.                         {
  142. #ifdef DEBUG
  143.                             case KEY_F1:    Trace=!Trace;
  144.                                                 break;
  145.                             case KEY_F2:    puts("\n*** REGISTERS: ***");        //F2 up
  146.                                                 for(J=0xFF40;J<0xFF50;J++)
  147.                                                     printf("(%Xh) = $%X\n",J,*(RAM+J));
  148.                                                 printf("\n");
  149.                                                 break;
  150. #endif
  151.                             case KEY_HELP:        EasyRequestArgs(NULL,&HelpReq,NULL,NULL);
  152.                             case KEY_DASH:        JoyState|=0x80;break;    //start
  153.                             case KEY_PERIOD:    JoyState|=0x40;break;    //select
  154.                             case KEY_Y:            JoyState|=0x20;break;    //B
  155.                             case KEY_X:            JoyState|=0x10;break;    //A
  156.                             case KEY_CDOWN:    JoyState|=0x08;break;    //down
  157.                             case KEY_CUP:        JoyState|=0x04;break;    //up
  158.                             case KEY_CLEFT:    JoyState|=0x02;break;    //left
  159.                             case KEY_CRIGHT:    JoyState|=0x01;break;    //right
  160.                         }
  161.                     } else
  162.                     {
  163.                         switch(Code)
  164.                         {
  165.                             case KEY_ESC:        ExitFlag = 1; break;
  166.                             case KEY_DASH:        JoyState&=0x7F;break;
  167.                             case KEY_PERIOD:    JoyState&=0xBF;break;
  168.                             case KEY_Y:            JoyState&=0xDF;break;
  169.                             case KEY_X:            JoyState&=0xEF;break;
  170.                             case KEY_CDOWN:    JoyState&=0xF7;break;
  171.                             case KEY_CUP:        JoyState&=0xFB;break;
  172.                             case KEY_CLEFT:    JoyState&=0xFD;break;
  173.                             case KEY_CRIGHT:    JoyState&=0xFE;break;
  174.                         }
  175.                     }
  176.             }
  177.         }
  178.  
  179.   if(ExitFlag)
  180.   { TrashGB();TrashMachine();exit(0); }
  181.  
  182.   return(JoyState);
  183. }
  184.  
  185. void AllocatePens(void)
  186. {
  187. int i;
  188. struct
  189. {
  190.     LONG r,g,b;
  191. } GB_Colors[4] = {{0xffffffff,0xffffffff,0xffffffff},{0x98ffffff,0x98ffffff,0x98ffffff},
  192.                         {0x58ffffff,0x58ffffff,0x58ffffff},{0x00ffffff,0x00ffffff,0x00ffffff}};
  193.  
  194.     for(i=0; i<4; i++)
  195.     {
  196.         Palette[i] = (UBYTE)(ObtainBestPenA(scr->ViewPort.ColorMap,GB_Colors[i].r,GB_Colors[i].g,GB_Colors[i].b,NULL));
  197.     }
  198. }
  199.  
  200. /** Common.h *************************************************/
  201. /** Parts of the drivers common for Unix/X and MSDOS.       **/
  202. /*************************************************************/
  203. #include "Common.h"
  204.